/*
 * Created on Oct 18, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package MethodCatcher;

import java.lang.reflect.Method;
import java.util.ArrayList;

import clock.AlarmClock;
import clock.AlarmClockTranslation;
import clock.Presentation;


/**
 * @author Jayanth Recherla
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class TestMethodCatcher{
	public static void main(String[] args) throws Exception {
		final AlarmClockTranslation translation = new AlarmClockTranslation();
		translation.setApplication( new AlarmClock(0,5));
		MethodCatcher catcher = new MethodCatcher();
		catcher.listenTo(translation);
		
		
		EventsGenerator eg = new EventsGenerator(translation);
		eg.fireAction("set");   	// Change the state to SET.
		for(int i=0;i<6;i++)
			eg.fireAction("minute");// Set the alarm time to 00:06 using hour/min
		eg.fireAction("on");    	// Change the state to ON
		for(int i=0;i<60;i++)
			eg.fireAction(null);  	// Generate 60 ticks so that current time 
						        	// increments by one min (Currently it is 00:05).
	    // NOW THE STATE GETS CHANGED TO ALARM_SOUND_STATE	
		display((ArrayList) catcher.getMethods(),(ArrayList) catcher.getArgs() );
		
		
		translation.setApplication( new AlarmClock(0,5));
		catcher = new MethodCatcher();
		catcher.listenTo(translation);
		
		eg = new EventsGenerator(translation);
		eg.fireAction("set");      // Change the state to SET.
		for(int i=0;i<5;i++) {
			eg.fireAction("hour");     // Set the alarm time to 06:06 
			eg.fireAction("minute");   // using hour/min actions
		}
		eg.fireAction("on");	   // Change the state to ON
		eg.fireMousePressed();     // Generate a mouse pressed event.
		for(int i=0;i<60;i++)
			eg.fireAction(null);   // Hold mouse for 5 ticks
		// NOW THE STATE GETS CHANGED TO ALARM_DISPLAY_STATE
		
		System.out.println(" ");
		System.out.println("========================================");
		display((ArrayList) catcher.getMethods(), (ArrayList) catcher.getArgs() );
		System.out.println(catcher.assertLastMethod(Presentation.class.getMethod("setAlarmDisplayState", new Class[0])));
	}
	
	private static void display(ArrayList methods , ArrayList arguments) {
		System.out.println("Methods Called Are::");
		int len = methods.size() ;
		for(int i=0; i<len ; i++) {
			System.out.print( ((Method)methods.get(i)).getName() + " ( ");
			if( arguments.get(i) != null )
			{
				Object[] arg = (Object[]) arguments.get(i) ;
				for(int j=0;j<arg.length ;j++)
				{
					if(j == (arg.length - 1))
						System.out.print(arg[j]);
					else 
						System.out.print(arg[j] + " , ");
				}
			}
			System.out.println(" )");
		}
	}
}
